package com.chinasofti.controller;

import com.chinasofti.service.DepartmentService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/addDeleteDepartmentServlet")
public class AddDeleteDepartmentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        DepartmentService service = new DepartmentService();


        if (code!=null && code.equals("add")){
            String departmentname = request.getParameter("departmentname");
            service.addDepartment(departmentname);
        }
        if(code != null && code.equals("delete")){
            String departmentid = request.getParameter("departmentid");
            service.deleteDepartment(Integer.parseInt(departmentid));
        }
        if(code != null && code.equals("edit")){
            String departmentid = request.getParameter("departmentid");
            String departmentname = request.getParameter("departmentname");
            service.updateNameById(Integer.parseInt(departmentid),departmentname);
        }
        response.sendRedirect("viewAllDepartmentServlet?code=viewalldepts");
    }
}
